** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_D10.smcl", replace

use "$project_path/data/4_regdata/regdata", clear

rdrobust diff_m1_growth_gdpc_penn runvar, all
rdrobust diff_m1_inflation_imf runvar, all
rdrobust diff_m1_unemployment_ilo runvar, all
rdrobust diff_m1_trade_wb runvar, all
rdrobust diff_m1_hdi runvar, all
rdrobust diff_m1_democracy_index runvar, all

log close

** Building .tex table

local variables diff_m1_growth_gdpc_penn diff_m1_inflation_imf diff_m1_unemployment_ilo diff_m1_trade_wb diff_m1_hdi diff_m1_democracy_index

//Generate a temporary dta file with variables description. We use an id variable that follows the order of the local `variables'.
preserve
gen description = ""

local i = 1
foreach v in `variables'{
	
	if "`v'" == "diff_m1_growth_gdpc_penn" local description = "GDP per capita growth"
	if "`v'" == "diff_m1_inflation_imf" local description = "Inflation"
	if "`v'" == "diff_m1_unemployment_ilo" local description = "Unemployment"
	if "`v'" == "diff_m1_trade_wb" local description = "Trade intensity"
	if "`v'" == "diff_m1_hdi" local description = "HDI"
	if "`v'" == "diff_m1_democracy_index" local description = "Democracy index"
	
	qui replace description = "`description'" in `i'
	
	local ++i
}
keep description 
drop if description == ""
gen id = _n
tempfile description_dta
save `description_dta'
restore

//Generate a temporary dta file with Unit variable
preserve
gen unit = ""
local i = 1
foreach v in `variables' {
	
	if "`v'" == "diff_m1_growth_gdpc_penn" local unit = "\%"
	if "`v'" == "diff_m1_inflation_imf" local unit = "\%"
	if "`v'" == "diff_m1_unemployment_ilo" local unit = "\% of labor force"
	if "`v'" == "diff_m1_trade_wb" local unit = "\% of GDP"
	if "`v'" == "diff_m1_hdi" local unit = "0-1 scale"
	if "`v'" == "diff_m1_democracy_index" local unit = "0-1 scale"
	
	qui replace unit = "`unit'" in `i'
	
	local ++i
	
}	
keep unit
drop if unit == ""
gen id = _n
tempfile unit_dta
save `unit_dta'
restore

//Prepare matrix of coefficients and other statistics from the regression
local rows : word count `variables'
local nrows `rows'
local ncols 4
mat Coefs = J(`nrows', `ncols', .)


local i = 1
foreach v in `variables' {


		rdrobust `v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i

}

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 estimates
rename Coefs2 std_err
rename Coefs3 pval
rename Coefs4 N

gen outcome = ""

gen id = _n
order id outcome, first

tostring estimates, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace estimates = estimates + "***" if pval < `3_star'
replace estimates = estimates + "**" if  pval < `2_star' & pval > `3_star'
replace estimates = estimates + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}

replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"

//Attach unit variable and description
merge 1:1 id using `unit_dta', assert(match) nogen
merge 1:1 id using `description_dta', assert(match) nogen

replace outcome = description

drop id description

label var outcome ""
label var estimates "Est."
label var std_err "SE"
label var pval "p-val."
label var N "N"
label var unit "Unit"

//Prepare texfile

local title "Effects on non-standardized outcomes"
local label_key "tab:quantitative_results"
local output "$project_path/output/appendix_tables/Appendix_Table_D10.tex"
local footnote = "\emph{Notes}: This table presents estimation results using the same outcomes and statistical procedure as in Table \ref{tab:index_components_baseline}, but outcomes are expressed in their original units instead of standard deviations."

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{6.8cm} p{1.7cm} p{1.5cm} p{1.5cm} p{1.3cm} p{3.2cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 
	

